package cn.gson.springboot.model.service.afterservice;

import cn.gson.springboot.model.dao.afterdao.*;
import cn.gson.springboot.model.dao.saledao.ExchangeDao;
import cn.gson.springboot.model.dao.saledao.ReturnorderDao;
import cn.gson.springboot.model.mapper.aftermapper.MaintainMapper;
import cn.gson.springboot.model.pjos.*;
import cn.gson.springboot.model.pjos.Process;
import cn.gson.springboot.model.vo.aftervo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class MaintainService {

    @Autowired
    MaintainMapper mapper;
    @Autowired
    MaintainDao dao;
    @Autowired
    MaintainDetailDao taildao;
    @Autowired
    ProcessDao prosdao;
    @Autowired
    InterflowDao infodao;
    @Autowired
    CourseDao courdao;
    @Autowired
    ExchangeDao exchangeDao;
    @Autowired
    ReturnorderDao returnDao;

    //分页查询维修工单
    public List<Maintain> selectPage(Integer stateId,String search){
        return mapper.tainPage(stateId,search);
    }

    //查询当天最大的商机名称
    public String selectMaxBusinessName(){
        SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMdd");
        Date date = new Date();//获取当前时间
        String code = mapper.selectMaxName();
        //如果查询出来的数字为空，则为当天第一个
        if(null == code || "".equals(code)){
            code = "S"+sdf.format(date)+"-"+"1";
        }else{
            //数字不为空则加一，
            int zhi = Integer.parseInt(code)+1;
            code = "S"+sdf.format(date)+"-"+zhi+"";
        }
        return code;
    }

    //新增维修工单
    public void addTain(MaintainVo tain){
        Timestamp time = new Timestamp(new Date().getTime());
        //修改退换货状态
        if(tain.getTainType().equals("换货") && tain.getTainNote() != null && tain.getTainNote() != ""){
            Integer excId = mapper.exchangeById(tain.getTainNote());
            Exchange change = exchangeDao.findById(excId).get();
            change.setExcState(0);//正在维修中
            exchangeDao.save(change);
        }
        if(tain.getTainType().equals("退货") && tain.getTainNote() != null && tain.getTainNote() != ""){
            Integer retuId = mapper.returnById(tain.getTainNote());
            Returnorder returnorder = returnDao.findById(retuId).get();
            returnorder.setRetuState(0);//正在维修中
            returnDao.save(returnorder);
        }
        //新增维修工单
        Maintain main = new Maintain();
        main.setTainId(tain.getTainId());
        main.setTainType(tain.getTainType());
        main.setTainNote(tain.getTainNote());
        main.setTainOrder(tain.getTainOrder());
        main.setCustomerByCustomId(tain.getCustomerByCustomId());
        main.setTainRece(tain.getTainRece());
        main.setTainState(tain.getTainState());
        main.setTainDelete(1);
        dao.save(main);
        //新增维修详表
        MaintainDetail details = new MaintainDetail();
        details.setMaintainByTainId(main);       //维修主表id
        details.setTainsId(tain.getTainsId());
        details.setTainsAlert(tain.getTainsAlert());
        details.setTainsClass(tain.getTainsClass());
        details.setTainsSection(tain.getTainsSection());
        details.setTainsRepair(tain.getTainsRepair());
        details.setTainsFault(tain.getTainsFault());
        details.setTainsAlert(tain.getTainsAlert());
        details.setTainsPerson(tain.getTainsPerson());
        details.setTainsPhone(tain.getTainsPhone());
        details.setTainsStatus(tain.getTainsStatus());
        details.setTainsRemark(tain.getTainsRemark());
        if(tain.getTainsDate() != null){
            details.setTainsDate(tain.getTainsDate());
        }else{
            details.setTainsDate(time);
        }
        Schedule sched = new Schedule();
        sched.setStateId(1);
        if(tain.getSchedu().getStateId() == 0){
            details.setSchedu(sched);
        }else {
            details.setSchedu(tain.getSchedu());
        }
        taildao.save(details);
    }

    //按维修主表id查询出维修详表id
    public Integer checkTainsId(Integer tainId){
        return mapper.selectTainsId(tainId);
    }

    //通过维修主表id找出维修过程表id
    public Integer checkCourId(Integer tainId){
        return mapper.checkByCourId(tainId);
    }

    //通过维修主表id找出客户沟通表id
    public Integer checkFlowId(Integer tainId){
        return mapper.checkByFlowId(tainId);
    }

    //维修进度修改为待维修
    public void changeTains(Integer tainId){
        if(tainId != null) {
            Integer tainsId = checkTainsId(tainId);
            MaintainDetail detail = taildao.findById(tainsId).get();
            Schedule sched = new Schedule();
            sched.setStateId(2);
            detail.setSchedu(sched);
            taildao.save(detail);
        }
    }

    //将维修进度修改为待确认
    public void changeTwo(Integer tainId){
        if(tainId != null) {
            Integer tainsId = checkTainsId(tainId);
            MaintainDetail detail = taildao.findById(tainsId).get();
            Schedule sched = new Schedule();
            sched.setStateId(3);
            detail.setSchedu(sched);
            taildao.save(detail);
        }
    }

    //将维修进度修改为待维修
    public void changeThree(Integer tainId){
        if(tainId != null) {
            Integer tainsId = checkTainsId(tainId);
            MaintainDetail detail = taildao.findById(tainsId).get();
            Schedule sched = new Schedule();
            sched.setStateId(4);
            detail.setSchedu(sched);
            taildao.save(detail);
            Integer flowId = checkFlowId(tainId);
            Interflow flow = infodao.findById(flowId).get();
            flow.setFlowResult("维修");
            infodao.save(flow);
        }
    }

    //确认不维修
    public void changeNo(Integer tainId){
        if(tainId != null) {
            Integer tainsId = checkTainsId(tainId);
            MaintainDetail detail = taildao.findById(tainsId).get();
            Schedule sched = new Schedule();
            sched.setStateId(7);
            detail.setSchedu(sched);
            taildao.save(detail);
            changeFour(tainId);
            Integer flowId = checkFlowId(tainId);
            Interflow flow = infodao.findById(flowId).get();
            flow.setFlowResult("不维修");
            infodao.save(flow);
            Maintain tain = new Maintain();
            tain.setTainId(tainId);
            Course cour = new Course();
            Timestamp time = new Timestamp(new Date().getTime());
            cour.setMaintainByTainId(tain);
            cour.setCourDate(time);
            cour.setCourDates(time);
            cour.setCourState("未完成");
            courdao.save(cour);
        }
    }

    //将维修进度修改为维修中
    public void changeFive(Integer tainId){
        if(tainId != null) {
            Integer tainsId = checkTainsId(tainId);
            MaintainDetail detail = taildao.findById(tainsId).get();
            Schedule sched = new Schedule();
            sched.setStateId(5);
            detail.setSchedu(sched);
            taildao.save(detail);
        }
    }

    //将维修进度修改为待沟通
    public void changeFour(Integer tainId){
        if(tainId != null) {
            Integer tainsId = checkTainsId(tainId);
            MaintainDetail detail = taildao.findById(tainsId).get();
            Schedule sched = new Schedule();
            sched.setStateId(6);
            detail.setSchedu(sched);
            taildao.save(detail);
        }
    }

    //将维修进度修改为维修完成
    public void changeSix(Integer tainId){
        if(tainId != null) {
            Integer tainsId = checkTainsId(tainId);
            MaintainDetail detail = taildao.findById(tainsId).get();
            Schedule sched = new Schedule();
            sched.setStateId(7);
            detail.setSchedu(sched);
            taildao.save(detail);
            Integer courId = checkCourId(tainId);
            Course cour = courdao.findById(courId).get();
            cour.setCourState("已完成");
            courdao.save(cour);
            Maintain tain = dao.findById(tainId).get();
            tain.setTainState(2);
            dao.save(tain);
        }
    }

    //通过维修主表id找出维修进度
    public Integer checkStateId(Integer tainIds){
        return mapper.checkByStateId(tainIds);
    }

    //新增检测表
    public void addPros(ProcessVo pros){
        Maintain main = new Maintain();
        main.setTainId(pros.getTainId());
        Process pro = new Process();
        pro.setMaintainByTainId(main);  //维修主表id
        pro.setProsId(pros.getProsId());
        pro.setProsCrew(pros.getProsCrew());
        pro.setProsData(pros.getProsData());
        pro.setProsDate(pros.getProsDate());
        pro.setProsDates(pros.getProsDates());
        pro.setProsExplain(pros.getProsExplain());
        prosdao.save(pro);
    }

    //新增客户沟通表
    public void addInfo(Interflow info){
        if(info.getFlowResult() == "" || info.getFlowResult() == null) {
            info.setFlowResult("确认中");
        }
        infodao.save(info);
    }

    //新增维修过程表
    public void addCour(Course cour){
        Timestamp time = new Timestamp(new Date().getTime());
        if(cour.getCourDate() == null){
            cour.setCourDate(time);
        }
        if(cour.getCourDates() == null){
            cour.setCourDates(time);
        }
        if (cour.getCourState() == "" || cour.getCourState() == null) {
            cour.setCourState("未完成");
        }
        courdao.save(cour);
    }

    //查询所有客户
    public List<Customer> customerAll(){
        return mapper.custAll();
    }

    //查询维修部门
    public List<Dept> selectDept(){
        return mapper.deptName();
    }

    //根据客户查出联系人
    public List<Culinkman> selectLink(Integer person){
        return mapper.linkName(person);
    }

    //回显维修主表
    public Maintain selectTain(Integer tainIds){
        return mapper.tainAll(tainIds);
    }

    //回显维修详表
    public MaintainDetail selectMain(Integer tainIds){
        return mapper.mainAll(tainIds);
    }

    //查询维修进度
    public List<Schedule> selectSchedu(){
        return mapper.duleAll();
    }

    //回显检测表
    public Process selectPros(Integer tainIds) {
        return mapper.prosAll(tainIds);
    }

    //回显客户沟通表
    public Interflow selectFlow(Integer tainIds){
        return mapper.flowAll(tainIds);
    }

    //回显维修过程表
    public Course selectCour(Integer tainIds){
        return mapper.courAll(tainIds);
    }

    //删除维修工单
    public void deleteByTainId(Integer tainId){
        if (tainId != null) {
            Maintain tain = dao.findById(tainId).get();
            tain.setTainDelete(0);
            dao.save(tain);
        }
    }

    //保修状况月度工单数分布
    public List<MaintainChartVo> selectRepairByMonth(){
        return mapper.repairByMonth();
    }

    //维修产品月度工单费用分布
    public List<PieDoubleVo> selectMoneyByMonth(){
        return mapper.moneyByMonth();
    }

    //维修完成月度工单数分布
    public List<Integer> selectDeliveredById(){
        return mapper.deliveredById();
    }

    //维修完成月度工单数分布的月份
    public List<String> selectDeliveredByMonth(){
        return mapper.deliveredByMonth();
    }

    //维修未完成月度工单数分布
    public List<Integer> selectUndeliveredByState(){
        return mapper.undeliveredByState();
    }

    //维修未完成月度工单数分布的月份
    public List<String> selectUndeliveredByMonth(){
        return mapper.undeliveredByMonth();
    }

    //工单月度分布
    public List<Integer> selectOrderByCount(){
        return mapper.orderByCount();
    }

    //工单月度分布的月份
    public List<String> selectOrderByMonth(){
        return mapper.orderByMonth();
    }

    //维修进度漏斗
    public List<MaintainChartVo> selectFunnelByState(){
        return mapper.funnelByState();
    }

    //维修总控台待检测
    public List<Maintain> selectTainByTwo(){
        return mapper.tainByTwo();
    }

    //维修总控台待确认
    public List<Maintain> selectTainByThree(){
        return mapper.tainByThree();
    }

    //维修总控台待维修
    public List<Maintain> selectTainByFour(){
        return mapper.tainByFour();
    }

    //维修总控台维修中
    public List<Maintain> selectTainByFive(){
        return mapper.tainByFive();
    }

    //维修总控台待沟通
    public List<Maintain> selectTainBySix(){
        return mapper.tainBySix();
    }

    //维修总控台维修完成
    public List<Maintain> selectTainBySeven(){
        return mapper.tainBySeven();
    }

    //维修总控台未收尽款
    public List<Maintain> selectTainByEight(){
        return mapper.tainByEight();
    }

    //通过维修单号查出维修主表id
    public Integer selectOrderByTainId(String tainOrder){
        return mapper.orderByTainId(tainOrder);
    }

    //根据退换货类型查出单号
    public List<Map<String,Object>> selectByType(String tainType){
        List<Map<String,Object>> list = null;
        if(tainType.equals("退货")){
            list = mapper.returnByOrder();
        }
        if(tainType.equals("换货")){
            list = mapper.changeByOrder();
        }
        return list;
    }

    //退换货单产品信息
    public List<MainProductVo> selectProductId(String tainType,String tainNote){
        List<MainProductVo> list = null;
        if(tainType.equals("换货") && tainNote != null && tainNote != ""){
            Integer excId = mapper.exchangeById(tainNote);
            list = mapper.productById(excId);
        }
        if(tainType.equals("退货") && tainNote != null && tainNote != ""){
            Integer retuId = mapper.returnById(tainNote);
            list = mapper.productByReturn(retuId);
        }
        System.out.println("李大哥"+list);
        return list;
    }

    //根据退换货单查询客户
    public List<Customer> customerById(String tainType,String tainNote){
        List<Customer> list = null;
        if(tainType.equals("换货") && tainNote != null && tainNote != ""){
            list = mapper.customerByChange(tainNote);
        }
        if(tainType.equals("退货") && tainNote != null && tainNote != ""){
            list = mapper.customerByReturn(tainNote);
        }
        return list;
    }

    //查询维修部员工
    public List<Emp> selectRepairEmp(){
        return mapper.repairByEmp();
    }
}
